
(function($){
	
	var Monitor = {
		
		//工具
		tool : {
			//数组去重
			delRepeat : function(arr){
			    var newArray=[];
			    var provisionalTable = {};
			    for (var i = 0, item; (item= arr[i]) != null; i++) {
			        if (!provisionalTable[item]) {
			            newArray.push(item);
			            provisionalTable[item] = true;
			        }
			    }
			    return newArray;
			},
		},
		
		//多选
		CheckItems : {
			options : {
				'link' : '.checkbox-column :checkbox',
				'output' : '#page',
				'data' : []
			},
			install : function(){
				$("#artOperateDiv").hide();
				this.handle();
				this.setState();
			},
			handle : function(){
				var self = this;
				$(self.options['link']).bind('click',function(){
					var me = $(this);
					
					if(me.attr('checked') == "checked"){												
						if(me.hasClass("art-checkbox")){
							me.parent().parent().addClass('current');
							self.opeara.selectOne(me);	
						}else{
							$(".mod-monitor-list tr").addClass('current')
							self.opeara.selectAll();	
						}
					}else{						
						if(me.hasClass("art-checkbox")){
							me.parent().parent().removeClass('current');
							self.opeara.unSelectOne(me);	
						}else{
							$(".mod-monitor-list tr").removeClass('current');
							self.opeara.unSelectAll();	
						}
					}
					
					self.action(me);
					self.save(me);								
				});	
			},
			
			//重置checkbox状态
			setState : function(){
				if($("#artOperateDiv").length < 1){
					return;
				}
				var self = this;
				
				var checkeds = self.options['data'];
				
				if(checkeds.length < 1){
					return;
				}
				
				$.each(checkeds,function(index,value){
					$(":checkbox[value="+ value +"]").attr('checked','checked');
				});				
			},
			
			//执行动作
			action : function(me){
				var self = this;	
				var _data = Monitor.CheckItems.options['data'];
				
				Monitor.CheckItems.options['data'] = Monitor.tool.delRepeat(_data);
				//Monitor.CheckItems.options['data'] = $.unique(Monitor.CheckItems.options['data']);				
				//console.dir(Monitor.CheckItems.options['data']);
					
				if(self.options['data'].length >= 1){
					//创建覆层
					if($("#artOperateDiv").length <= 0){
						self.build();					
					}
					
					self.show(me);
				}else{
					//清除覆层
					$("#artOperateDiv").hide();
				}
								
			},
					
			opeara : {
				//选中1项
				selectOne : function(me){
					var _item = me.val();
					Monitor.CheckItems.options['data'].push(_item);
				},
				
				//选中所有
				selectAll : function(){
					$.each($(".art-checkbox"),function(index,_item){												
						Monitor.CheckItems.options['data'].push(_item.value);	
					});
				},
				
				//取消选中1项
				unSelectOne : function(me){
					var data = Monitor.CheckItems.options['data'];
					var _item = me.val();
					
					$.each(data,function(index,_data){
						if(_data == _item){
							Monitor.CheckItems.options['data'].splice(index,1);	
							return;
						}
					});
					
				},				
				
				//取消所有选中
				unSelectAll : function(){
					//Monitor.CheckItems.options['data'] = [];	
					var _data = Monitor.CheckItems.options['data'];
					
					$.each($(".art-checkbox"),function(ci,_item){												
						//Monitor.CheckItems.options['data'].push(_item.value);	
						$.each(_data,function(ai,_d){
							if(_d == _item.value){
								Monitor.CheckItems.options['data'].splice(ai,1);		
							}
						})
						
					});
					
				},
				
								
				//下载
				download : function(){					
					$("#link-down").bind('click',function(){
						var ids = Monitor.CheckItems.options['data'].join(','),
							baud = $("select#Setting_BAUD_V").val(),
							bits = $("select#Setting_DATABITS_N").val(),
							sbits = $("select#Setting_STOPBITS_N").val(),
							parity = $("select#Setting_PARITY_V").val();
						
						
						$.ajax({
							'url' : 'index.php',
							'data' : {
								'r' : 'sensors/command',
								'baud' : baud,
								'databits' : bits,
								'stopbits' : sbits,
								'parity' : parity,
								'ids' : ids	
							},
							'type' : 'post',
							'success' : function(){
							
							}	
						});
						
						//return true; 
					});
				},
				
				
				
				//关闭
				_close : function(){
					$("#link-close").bind('click',function(){
						$("#artOperateDiv").hide();
					});	
				}			
			},
			save : function(me){
				var self = this;
				var _data = self.options['data'].join('_');
				$("#artOperateDiv").find("dd").attr('data',_data);
				
			},
			build : function(){
				var self = this;
				var template = ["<dl>",								
								'<dt><a id="link-close" href="javascript:void(0);">',
								'<span class="ui-icon ui-icon-close"></span></a>可进行以下操作</dt>',
								"<dd>",
								'<a id="link-down" href="javascript:void(0);"><span class="ui-icon ui-icon-circle-arrow-s"></span>采集</a>',
								//'<a id="link-del" href="javascript:void(0);"><span class="ui-icon ui-icon-trash"></span>删除</a>',
								"</dd>",
								"</dl>"].join("");
				
				
				self.options['container'] = $('<div>').attr('id','artOperateDiv').html(template);
				
				$(self.options['output']).append(self.options['container']);
				
				self.opeara.download();
				self.opeara._close();
				
			},
			show : function(me){				
				var self = this;
				var alls = self.options['data'];
				
				var nextSelector = function(){
					return $(":checkbox[value="+ alls[alls.length-1] +"]");
				};
				
				var offset = (me.attr('checked')=="checked") ? me.offset() : nextSelector().offset();
				
				if(offset){				
					$("#artOperateDiv").css({
						'display' : 'block',
						'top' : offset.top - 101,
						'left' : offset.left + 20
					});	
				}
			}
		},	
		
		//时间段
		sdate : {
			options : {
				'link' : 'select[name=sdate]',
				'output' : 'input.hasDatepicker'
			},
			install : function(){
				this.bindle();
			},
			bindle : function(){
				var self = this;
				$(self.options['link']).bind('change',function(){
					var sType = this.value;						
					var times = (sType=='all')? {beigin:'',end:''} : Monitor.tool.getDateByShuttle(sType);					
					
					$("input[name=begin]").val(times.begin);
					$("input[name=end]").val(times.end);
				});
			}
		},

		//页面初始化
		init : function(){
			this.CheckItems.install();		//多选组件
		}
			
	};
	
	$.Monitor = Monitor;
	
	Monitor.init();
})(jQuery);
